package com.glela.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.glela.common.util.LinkUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.glela.common.constant.BaseConstants;
import com.glela.platform.constant.ErrorEnum;
import com.glela.platform.model.BaseRestrictions;
import com.glela.platform.model.StoreTopBanner;
import com.glela.platform.model.User;
import com.glela.platform.model.request.StoreTopBannerRequest;
import com.glela.platform.model.response.ResponseBaseModel;
import com.glela.platform.model.response.ResponseResultModel;
import com.glela.platform.model.response.StoreTopBannerResponse;
import com.glela.platform.service.BaseRestrictionsService;
import com.glela.platform.service.StoreTopBannerService;
import com.glela.platform.service.UserService;
import com.glela.platform.util.ParameterUtils;

/**
 * 
* @ClassName: StoreTopBannerController
* @Description: 【店主精选】业务入口
* @author NingChongQing
* @date 2017年7月7日 上午9:36:24
 */
@Controller
@RequestMapping("storeTopBanner")
public class StoreTopBannerController {
	private static Logger logger = LoggerFactory.getLogger(StoreTopBannerController.class);
	@Autowired
	private StoreTopBannerService storeTopBannerService;
	@Autowired
	private UserService userService;
	@Autowired
	private BaseRestrictionsService baseRestrictionsService;
	/**
	 * 
	* **********************************************
	* @Title: getStoreTopBanner
	* @Description: 查找店主精选顶部图片
	* @param @param request
	* @param @return    设定文件
	* @return ResponseEntity<?>    返回类型
	* @throws		异常
	* @author NingChongQing		作者
	* @date 2017年7月7日 上午11:27:04		时间
	* **********************************************
	 */
	@RequestMapping(value = "/getStoreTopBanner", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> getStoreTopBanner(@RequestBody StoreTopBannerRequest request)
	{
		try {
			//店主用户ID  默认校妆姐姐店铺的店主ID
			Integer storeUserId = Integer.valueOf(BaseConstants.XZ_SISTER+"");
			// 显示当前登录或者分享人的店铺信息
			Long showUserId = 0L;
			if (!ParameterUtils.validParams(request.getUserId())) {// 当前登录人
				showUserId = request.getUserId();
			} else if (!ParameterUtils.validParams(request.getTalentId())) {// 分享人
				showUserId = Long.valueOf(request.getTalentId());
			}
			// 没有绑定店主且不是从分享链接查看店主精选商品列表
			if (showUserId.intValue() != 0) {
				User user = this.userService.getUserById(showUserId);
				User storeUser =this.userService.getUserByPersionId(user.getRecommendFrom());
				storeUserId = storeUser.getId().intValue();
			}
			//店铺Banner图类型
			Integer storeInfoType = 0;
			if (!ParameterUtils.validParams(request.getStoreInfoType())) {
				storeInfoType = request.getStoreInfoType();
			}
			StoreTopBanner storeTopBanner = new StoreTopBanner();
			storeTopBanner.setStoreUserId(storeUserId);
			if(storeInfoType != 0){
				storeTopBanner.setStoreInfoType(storeInfoType);
			}
			storeTopBanner.setIsDelete(0);
			List<StoreTopBanner> StoreTopBannerList = this.storeTopBannerService.findStoreTopBannerList(storeTopBanner);
			if(CollectionUtils.isEmpty(StoreTopBannerList)){
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C22201), HttpStatus.OK);
			}
			return new ResponseEntity<>(new ResponseResultModel<StoreTopBannerResponse>(new StoreTopBannerResponse(StoreTopBannerList.get(0))), HttpStatus.OK);
		} catch (Exception e) {
			logger.error("查询店主精选顶部图片失败", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	
	/**
	* **********************************************
	* @Title: shareStore 
	* @Description: 分享店铺 
	* @param @param request
	* @param @return 
	* @return ResponseEntity<?>
	* @throws	
	* @author NingChongQing	
	* @date 2018年1月10日 下午3:50:07 
	* **********************************************
	 */
	@RequestMapping(value = "/shareStore", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> shareStore(@RequestBody StoreTopBannerRequest request)
	{
		try {
			if (ParameterUtils.validParams(request.getUserId())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			User user = this.userService.getUserById(request.getUserId());
			/*		User storeUser =this.userService.getUserByPersionId(user.getRecommendFrom());*/
			long storeUserId = user.getId();
			BaseRestrictions imageBR = this.baseRestrictionsService.getBaseRestrictionsForCache("image_url");
			int size = imageBR.getExperience();
			StringBuffer storeUserHeaderPicture = new StringBuffer();
			storeUserHeaderPicture.append(imageBR.getName());
			storeUserHeaderPicture.append(user.getPicture());
			storeUserHeaderPicture.append("?imageView2/0/w/");
			storeUserHeaderPicture.append(size);
			storeUserHeaderPicture.append("/h/");
			storeUserHeaderPicture.append(size);
			String h5_link = this.baseRestrictionsService.getBaseRestrictionsForCache("h5_link").getName();
			StringBuffer shareURL = new StringBuffer();
			shareURL.append(h5_link);
			shareURL.append("/index.html?talentId="+storeUserId);
			Map<String, Object> map = new HashMap<>();
			map.put("storeUserName", user.getNickName());
			map.put("storeUserHeaderPicture", storeUserHeaderPicture.toString());
			map.put("shareURL", LinkUtil.generateSinaShortUrl(shareURL.toString()));
			return new ResponseEntity<>(new ResponseResultModel<Map<String, Object>>(map), HttpStatus.OK);
		} catch (Exception e) {
			logger.error("分享店铺失败", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	
}
